package com.ruoyi.web.controller.business;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.HurtModelResult;
import com.ruoyi.business.service.IHurtModelResultService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 3D疼痛模型结果Controller
 *
 * @author ruoyi
 * @date 2024-04-20
 */
@RestController
@RequestMapping("/business/modelResult")
public class HurtModelResultController extends BaseController
{
    @Autowired
    private IHurtModelResultService hurtModelResultService;

    /**
     * 查询3D疼痛模型结果列表
     */
    @PreAuthorize("@ss.hasPermi('business:modelResult:list')")
    @GetMapping("/list")
    public TableDataInfo list(HurtModelResult hurtModelResult)
    {
        startPage();
        List<HurtModelResult> list = hurtModelResultService.selectHurtModelResultList(hurtModelResult);
        return getDataTable(list);
    }

    /**
     * 导出3D疼痛模型结果列表
     */
    @PreAuthorize("@ss.hasPermi('business:modelResult:export')")
    @Log(title = "3D疼痛模型结果", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HurtModelResult hurtModelResult)
    {
        List<HurtModelResult> list = hurtModelResultService.selectHurtModelResultList(hurtModelResult);
        ExcelUtil<HurtModelResult> util = new ExcelUtil<HurtModelResult>(HurtModelResult.class);
        util.exportExcel(response, list, "3D疼痛模型结果数据");
    }

    /**
     * 获取3D疼痛模型结果详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:modelResult:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(hurtModelResultService.selectHurtModelResultById(id));
    }

    /**
     * 新增3D疼痛模型结果
     */
    @PreAuthorize("@ss.hasPermi('business:modelResult:add')")
    @Log(title = "3D疼痛模型结果", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HurtModelResult hurtModelResult)
    {
        return toAjax(hurtModelResultService.insertHurtModelResult(hurtModelResult));
    }

    /**
     * 修改3D疼痛模型结果
     */
    @PreAuthorize("@ss.hasPermi('business:modelResult:edit')")
    @Log(title = "3D疼痛模型结果", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HurtModelResult hurtModelResult)
    {
        return toAjax(hurtModelResultService.updateHurtModelResult(hurtModelResult));
    }

    /**
     * 删除3D疼痛模型结果
     */
    @PreAuthorize("@ss.hasPermi('business:modelResult:remove')")
    @Log(title = "3D疼痛模型结果", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(hurtModelResultService.deleteHurtModelResultByIds(ids));
    }

    /**
     * 新增患者3d疼痛模型记录
     * @param
     * @return
     */
    @PostMapping("/savePatientModel")
    public AjaxResult savePatientModel(@RequestBody String json){
        System.out.println("3d疼痛模型数据"+json);
        HurtModelResult hurtModelResult = JSONObject.parseObject(json, HurtModelResult.class);
        Long patientId = SecurityUtils.getUserId();
        hurtModelResult.setPatientId(patientId);
        return toAjax(hurtModelResultService.insertHurtModelResult(hurtModelResult));
    }

    /**
     * 患者获取疼痛模型结果
     * @return
     */
    @GetMapping("/getPatientModelResult")
    public AjaxResult getPatientModelResult(){
        Long patientId = SecurityUtils.getUserId();
        List<Map<String, Object>> list = hurtModelResultService.getPatientModelResult(patientId);
        return AjaxResult.success(list);
    }

    /**
     * 患者获取疼痛模型结果详情
     * @return
     */
    @GetMapping("/getPatientModelDetail")
    public AjaxResult getPatientModelDetail(@RequestParam Long id){
        Map<String,Object> resultMap=new HashMap<>();
        Map<String, String> map = hurtModelResultService.getPatientModelDetail(id);
        if (map==null){
            return AjaxResult.success(resultMap);
        }else {
            String result = map.get("result");
            List list = JSONObject.parseObject(result, List.class);
            resultMap.put("number", map.get("number"));
            resultMap.put("result",list);

            resultMap.put("shoulder",map.get("shoulder"));
            resultMap.put("neck",map.get("neck"));
            resultMap.put("waist",map.get("waist"));
            resultMap.put("hip",map.get("hip"));
            resultMap.put("knee",map.get("knee"));
            resultMap.put("ankle",map.get("ankle"));
            return AjaxResult.success(resultMap);
        }

    }

    /**
     * 医师获取患者疼痛模型结果集
     * @return
     */
    @GetMapping("/getPatientModelList")
    public AjaxResult getPatientModelList(@RequestParam Long patientId){
        List<Map<String, Object>> list = hurtModelResultService.getPatientModelList(patientId);
        return AjaxResult.success(list);
    }
}
